// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/functions/sparksql/registration/register.h>
#include <pollux/expression/simple_function_registry.h>
#include <pollux/expression/special_form_registry.h>

namespace kumo::pollux::functions::sparksql {
    extern void registerArrayFunctions(const std::string &prefix);

    extern void registerBinaryFunctions(const std::string &prefix);

    extern void registerBitwiseFunctions(const std::string &prefix);

    extern void registerCompareFunctions(const std::string &prefix);

    extern void registerDatetimeFunctions(const std::string &prefix);

    extern void registerJsonFunctions(const std::string &prefix);

    extern void registerMapFunctions(const std::string &prefix);

    extern void registerMathFunctions(const std::string &prefix);

    extern void registerMiscFunctions(const std::string &prefix);

    extern void registerRegexpFunctions(const std::string &prefix);

    extern void registerSpecialFormGeneralFunctions(const std::string &prefix);

    extern void registerStringFunctions(const std::string &prefix);

    extern void registerUrlFunctions(const std::string &prefix);

    void registerFunctions(const std::string &prefix) {
        registerArrayFunctions(prefix);
        registerBinaryFunctions(prefix);
        registerBitwiseFunctions(prefix);
        registerCompareFunctions(prefix);
        registerDatetimeFunctions(prefix);
        registerJsonFunctions(prefix);
        registerMapFunctions(prefix);
        registerMathFunctions(prefix);
        registerMiscFunctions(prefix);
        registerRegexpFunctions(prefix);
        registerSpecialFormGeneralFunctions(prefix);
        registerStringFunctions(prefix);
        registerUrlFunctions(prefix);
    }

    std::vector<std::string> listFunctionNames() {
        std::vector<std::string> names =
                exec::specialFormRegistry().getSpecialFormNames();

        const auto &functions = exec::simple_functions().get_function_names();
        names.insert(names.end(), functions.begin(), functions.end());

        exec::vectorFunctionFactories().withRLock([&](const auto &map) {
            names.reserve(names.size() + map.size());
            for (const auto &[name, _]: map) {
                names.push_back(name);
            }
        });

        return names;
    }
} // namespace kumo::pollux::functions::sparksql
